package com.noahplat.device.controller;

import com.noahplat.common.utils.Result;
import com.noahplat.device.pojo.Type;
import com.noahplat.device.service.TypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * @ClassName: TypeController
 * @Description: 设备类型controller
 * @Author 李景星
 * @Date 2021/3/9
 * @Version 1.0
 */
@RestController
@RequestMapping("/api/type")
@Validated
public class TypeController {

    @Autowired
    private TypeService typeService;

    /**
     * 获取所有设备类型
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:TypeController:getType')")
    @GetMapping("/getType")
    public Result getType(){
        return typeService.getType();
    }

    /**
     * 添加设备类型
     * @param type
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:TypeController:addType')")
    @PostMapping("/addType")
    public Result addType(@RequestBody Type type){
        return typeService.addType(type);
    }

    /**
     * 通过ID查询设备类型
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:TypeController:getById')")
    @GetMapping("/getById")
    public Result getById(@NotNull(message = "类型ID不能为空 ") @Min(value = 1, message = "类型Id不能为空")Long id){
        return typeService.getById(id);
    }

    /**
     * 分页查询设备类型
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:TypeController:selectType')")
    @GetMapping("/selectType")
    public Result selectType(@RequestParam(defaultValue = "1")Integer page,@RequestParam(defaultValue = "10")Integer limit){
        return typeService.selectType(page, limit);
    }

    /**
     * 修改设备类型
     * @param type
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:TypeController:updateType')")
    @PutMapping("/updateType")
    public Result updateType(@RequestBody Type type){
        return typeService.updateType(type);
    }

    /**
     * 删除设备类型
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-device:TypeController:delType')")
    @DeleteMapping("/delType")
    public Result delType(@NotNull(message = "型号ID不能为空 ") @Min(value = 1, message = "型号Id不能为空")Long id){
        return typeService.delType(id);
    }


}
